package com.hafiery.sdk.bluetooth.transport.engine;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import com.hafiery.sdk.bluetooth.transport.callback.CallBackHandler;
import com.hafiery.sdk.bluetooth.transport.req.RequestData;
import com.hafiery.sdk.bluetooth.transport.res.ResponseData;
import com.hafiery.sdk.bluetooth.util.LogUtil;

public class Engine implements IEngineAction{
    private BluetoothGatt bluetoothGatt;
    private BluetoothGattCharacteristic charaWrite;
    public Engine(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic charaWrite){
        this.bluetoothGatt = bluetoothGatt;
        this.charaWrite=charaWrite;
    }
    @Override
    public <T> void send(RequestData requestData, CallBackHandler<T> callBack) {
        byte[] receivedBytes=requestData.getByteData();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < receivedBytes.length; i++) {
            sb.append(String.format("%02x", receivedBytes[i]));
        }

        LogUtil.log("发送数据:"+sb);
        charaWrite.setValue(requestData.getByteData());
        bluetoothGatt.writeCharacteristic(charaWrite);
    }

    @Override
    public void received(ResponseData responseData) {

    }
}
